package com.proton.temp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.proton.temp.connector.TempConnectorManager;
import com.proton.temp.connector.bean.DeviceBean;
import com.proton.temp.connector.bluetooth.BleConnector;
import com.proton.temp.connector.bluetooth.callback.OnScanListener;
import com.proton.temp.connector.interfaces.ConnectStatusListener;
import com.proton.temp.connector.interfaces.DataListener;
import com.wms.ble.utils.BluetoothUtils;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView mTempTxt;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PermissionUtils.getLocationPermission(this);
        PermissionUtils.getReadAndWritePermission(this);
        mTempTxt = findViewById(R.id.id_temp);

        //todo 搜索设备需要定位权限，android6.0及其以上需要动态申请权限
        //todo 或者手机权限管理里面直接允许定位权限
        final TempConnectorManager manager = TempConnectorManager.getInstance("你要连接的体温贴mac地址");
        //todo 如果是通过底座去连接，则调用TempConnectorManager.getNetConnectInstance("底座的mac地址，注意不是贴的mac地址")
        //todo 或者调用TempConnectorManager.getInstance(new DeviceBean("底座mac地址", false))，false代表不是蓝牙连接

        if (!BluetoothUtils.isBluetoothOpened()) {
            BluetoothUtils.openBluetooth();
        }
        findViewById(R.id.id_search_device).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo 需要指定扫描时间可以用两个参数的扫描方法,单位秒
                BleConnector.scanDevice(new OnScanListener() {

                    @Override
                    public void onScanStart() {
                        Log.e(TAG, "onScanStart...");
                    }

                    @Override
                    public void onDeviceFound(DeviceBean deviceBean) {
                        Log.w(TAG, "onDeviceFound: " + deviceBean.getMacaddress());
                    }

                    @Override
                    public void onScanStopped() {
                        Log.e(TAG, "onScanStopped...");
                    }

                    @Override
                    public void onScanCanceled() {
                        Log.e(TAG, "onScanCanceled...");
                    }
                });
            }
        });

        findViewById(R.id.id_start_measure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConnectStatusListener connectStatusListener = new ConnectStatusListener() {
                    @Override
                    public void onConnectSuccess() {
                        Log.w(TAG, "连接成功");
                    }

                    @Override
                    public void onConnectFaild() {
                    }

                    @Override
                    public void onDisconnect() {
                        Log.w(TAG, "连接断开");
                    }
                };

                DataListener dataListener = new DataListener() {
                    @Override
                    public void receiveCurrentTemp(float currentTemp) {
                        Log.w(TAG, "receiveCurrentTemp: 当前温度:" + currentTemp);
                        mTempTxt.setText(String.valueOf(currentTemp));
                    }

                    @Override
                    public void receiveBattery(Integer battery) {
                        Log.w(TAG, "receiveBattery: 当前电量:" + battery);
                    }

                    @Override
                    public void receiveCharge(boolean isCharge) {
                        Log.w(TAG, "receiveCharge: 是否充电:" + isCharge);
                    }

                    @Override
                    public void receiveSerial(String serial) {
                        Log.w(TAG, "receiveSerial: 序列号:" + serial);
                    }

                    @Override
                    public void receiveHardVersion(String hardVersion) {
                        Log.w(TAG, "receiveHardVersion: 固件版本:" + hardVersion);
                    }
                };
                manager.connect(connectStatusListener, dataListener, true);
            }
        });


        findViewById(R.id.id_end_measure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.disConnect();
            }
        });
    }
}
